package com.yzd.learnjava.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.yzd.learnjava.util.Print;

public class SerialCtl implements Serializable{
	private String a ;
	private transient String b;
	public SerialCtl(String aa , String bb) {
		a = "Not Transiemt " + aa;
		b = "Transient " + bb;
	}
	
	public String toString() {
		return a + "\n" + b;
	}
	
	private void writeObject(ObjectOutputStream out) throws IOException {
		out.defaultWriteObject();
		out.writeObject(b);
	}
	
	private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
		in.defaultReadObject();
		b = (String)in.readObject();
	}
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		SerialCtl sc = new SerialCtl("Test1", "Test2");
		Print.print("Before : \n" + sc);
		
		ByteArrayOutputStream bao = new ByteArrayOutputStream();
		ObjectOutputStream o = new ObjectOutputStream(bao);
		o.writeObject(sc);
		
		ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
		SerialCtl c2 = (SerialCtl)in.readObject();
		Print.print("Afetr : \n" + c2);
	}
}
